<?php

namespace app\common\repository\article;

use think\Db;
use app\common\model\book\BookSection as BookSectionModel;
use app\common\model\article\Article as ArticleModel;

class U
{
    public function __construct(
        ArticleModel $articleModel,
        BookSectionModel $bookSectionModel
    ) {
        $this->articleModel = $articleModel;
        $this->bookSectionModel = $bookSectionModel;
    }

    public function updateArticle($entity, $cond)
    {
        $this->articleModel->allowField(true)->save($entity, $cond);
        return $this->articleModel;
    }

    public function updateArticleTag($tag, $articleId)
    {
        $deletePivotTagSql = '
            DELETE FROM
                article_access_article_tag
            WHERE
                article_id = :id
        ';
        Db::execute($deletePivotTagSql, ['id' => $articleId]);
        ArticleModel::get($articleId)->tag()->saveAll($tag);
    }

    public function updateArticleType($type, $articleId)
    {
        $deletePivotTypeSql = '
            DELETE FROM
                article_access_article_type
            WHERE
                article_id = :id
        ';
        Db::execute($deletePivotTypeSql, ['id' => $articleId]);
        ArticleModel::get($articleId)->type()->saveAll($type);
    }

    public function updateArticleKnow($know, $articleId)
    {
        $deleteArticleKnowSql = '
            DELETE FROM
                article_know
            WHERE
                article_id = :id
        ';
        Db::execute($deleteArticleKnowSql, ['id' => $articleId]);
        ArticleModel::get($articleId)->know()->saveAll($know);
    }

    public function updateArticleSource($source, $articleId)
    {
        $deleteArticleSourceSql = '
            DELETE FROM
                article_source
            WHERE
                article_id = :id
        ';
        Db::execute($deleteArticleSourceSql, ['id' => $articleId]);
        ArticleModel::get($articleId)->source()->saveAll($source);
    }

    public function updateArticleGoods($goods, $articleId)
    {
        $deleteArticleGoodsSql = '
            DELETE FROM
                article_goods
            WHERE
                article_id = :id
        ';
        Db::execute($deleteArticleGoodsSql, ['id' => $articleId]);
        ArticleModel::get($articleId)->goods()->saveAll($goods);
    }

    public function updateArticleSection($sectionId, $articleId)
    {
        Db::table('article_access_book_section')
            ->where('article_id', $articleId)->delete();
        if (!empty($sectionId)) {
            ArticleModel::get($articleId)
                ->section()->save($sectionId);
        }
    }

    public function doArticlePraise($id)
    {
        $doPraiseSql = '
            UPDATE
                article
            SET
                praise_count = praise_count + 1
            WHERE
                id = :id
            ';
        Db::execute($doPraiseSql, ['id' => $id]);
    }

    public function doArticleView($id)
    {
        $doViewSql = '
            UPDATE
                article
            SET
                view_count = view_count + 1
            WHERE
                id = :id
            ';
        Db::execute($doViewSql, ['id' => $id]);
    }

    public function doArticleComment($id)
    {
        $doCommentSql = '
            UPDATE
                article
            SET
                comment_count = comment_count + 1
            WHERE
                id = :id
            ';
        Db::execute($doCommentSql, ['id' => $id]);
    }
}
